<?php
namespace app\controller;
use think\Request;
use think\View;
use app\model\Alg as AlgModel;
use app\model\Adv as AdvModel;
use app\model\Info as InfoModel;
use app\common\BigWhite;
class Index
{
    // 预测页面
    public function index(Request $request)
    {
        $data   =   $request->param();
        if (!$request->has('type','get')){
            $data['type']=1;
        }
        if (!$request->has('kjtype','get')){
            $data['kjtype']=1;
        }
        if (!$request->has('i','get')){
            $data['i']=1;
        }
        $c1=array();
        $c2=array();
        $c3=array();
        $c4=array();
        $qh=array();
        $sj=array();
        $kj=array();
        $z=array();
        $ycdata=array(); // 页面显示的预测数据
        $fgsf=array(); // 解析算法数组
        $sftj=array(); // 算法推荐
        $z=array(); // 值
        $b=array();// 算法结果储存数组
        $dc=array();// 对错类型
        $maxlz=array(); //最大连中
        $maxlc=array(); //最大连错
        $zq=array();// 正确期数
        $cw=array();// 错误期数
        $bg     =   new BigWhite();
        $alg    =   AlgModel::select();
        $adv    =   AdvModel::select();
        $info   =   InfoModel::select();
        // 判断开奖类型
        if ($data['kjtype']==1){
            $kjdata   =   $bg->GetJnd();
        }
        if ($data['kjtype']==2){
            $kjdata   =   $bg->GetTw();
        }
        if ($data['kjtype']==3){
            $kjdata   =   $bg->GetAz();
        }
        // 解析算法开始
        for ($y=0;$y<count($alg);$y++){
            $fgsf[$y]=[];
            $fg1=$bg->Analysis(',',$alg[$y]['alg']);
            for ($k=0;$k<count($fg1);$k++){
                $fgsf[$y][$k]=[];
                $fg2=$bg->Analysis('+',$fg1[$k]);
                for ($h=0;$h<count($fg2);$h++){
                    array_push($fgsf[$y][$k],$fg2[$h]);
                }
            }
        }
        // 开始获取算法结果
        for ($y=0;$y<count($fgsf);$y++){
            $b[$y]=[];
                for($i=0;$i<103;$i++){
                    $z[0]=0;
                    $z[1]=$kjdata[$i]['c1'];
                    $z[2]=$kjdata[$i]['c2'];
                    $z[3]=$kjdata[$i]['c3'];
                    $z[4]=$kjdata[$i+1]['c1'];
                    $z[5]=$kjdata[$i+1]['c2'];
                    $z[6]=$kjdata[$i+1]['c3'];
                    $z[7]=$kjdata[$i+2]['c1'];
                    $z[8]=$kjdata[$i+2]['c2'];
                    $z[9]=$kjdata[$i+2]['c3'];
                    $z[10]=$kjdata[$i+3]['c1'];
                    $z[11]=$kjdata[$i+3]['c2'];
                    $z[12]=$kjdata[$i+3]['c3'];
                    $n=0;
                    for ($k=0;$k<count($fgsf[$y]);$k++){
                        $m=0;
                        for ($h=0;$h<count($fgsf[$y][$k]);$h++){
                            $m += intval($bg->GetWs($z[$fgsf[$y][$k][$h]]));
                        }
                        $n+=intval($bg->GetWs($m));
                    }
                    array_push($b[$y],$n);
            }
        }
//        dd($b);
        // 判断推荐
        for ($y=0;$y<count($fgsf);$y++){
            $lz=0;
            $lc=0;
            $ycdata[$y]=[];
            $maxlz[$y]=0;
            $maxlc[$y]=0;
            $dc[$y]=[];
            $zq[$y]=0;
            $cw[$y]=0;
            for($i=0;$i<101;$i++){
                // 预测模式为组合
                if ($data['type']==1){
                    // 预测文本
                    $ycdata[$y][$i]=$bg->GetDx($b[$y][$i]).$bg->GetDs($b[$y][$i]);
                    // 组合判断
                    if ($bg->GetDs($b[$y][100-$i+1])!== $bg->GetDs($kjdata[100-$i]['c4']) and $bg->GetDx($b[$y][100-$i+1])!== $bg->GetDx($kjdata[100-$i]['c4'])){
                       // 组合错误
                        $dc[$y][100-$i]=0;
                        $lz=0;
                        $lc++;
                    }else if($bg->GetDs($b[$y][100-$i+1]) === $bg->GetDs($kjdata[100-$i]['c4']) and $bg->GetDx($b[$y][100-$i+1])=== $bg->GetDx($kjdata[100-$i]['c4'])){
                        // 组合正确
                        $dc[$y][100-$i]=1;
                        $lz++;
                        $lc=0;
                        $zq[$y]++;
                    }else if($bg->GetDs($b[$y][100-$i+1]) === $bg->GetDs($kjdata[100-$i]['c4'])){
                        // 单双正确
                        $dc[$y][100-$i]=2;
                        $lz++;
                        $lc=0;
                        $zq[$y]++;
                    }else{
                        // 大小正确
                        $dc[$y][100-$i]=3;
                        $lz++;
                        $lc=0;
                        $zq[$y]++;
                    }
                }
                // 预测模式为杀组合
                if ($data['type']==2){
                    // 预测文本
                    $ycdata[$y][$i]='杀'.$bg->GetZh($b[$y][$i]);
                    // 杀组合判断
                    if ($bg->GetZh($b[$y][100-$i+1]) === $bg->GetZh($kjdata[100-$i]['c4'])){
                        $dc[$y][100-$i]=0;
                        $lz=0;
                        $lc++;
                    }else{
                        $dc[$y][100-$i]=1;
                        $lz++;
                        $lc=0;
                        $zq[$y]++;
                    }
                }
                // 预测模式为
                if ($data['type']==4){
                    // 龙虎
                    if($bg->Get3y($b[$y][$i])=='0'){
                        $ycdata[$y][$i]='龙';
                    }
                    if($bg->Get3y($b[$y][$i])=='1'){
                        $ycdata[$y][$i]='虎';
                    }
                    if($bg->Get3y($b[$y][$i])=='2'){
                        $ycdata[$y][$i]='豹';
                    }
                    if ($bg->Get3y($b[$y][100-$i+1]) !== $bg->Get3y($kjdata[100-$i]['c4'])){
                        $lz=0;
                        $dc[$y][100-$i]=0;
                        $lc++;
                    }else{
                        $lz++;
                        $dc[$y][100-$i]=1;
                        $lc=0;
                        $zq[$y]++;
                    }
                }
                // 预测模式为杀四余
                if ($data['type']==5){
                    // 杀四余判断
                    $ycdata[$y][$i]='杀4N'.$bg->Get4y($b[$y][$i]);
                    if ($bg->Get4y($b[$y][100-$i+1]) === $bg->Get4y($kjdata[100-$i]['c4'])){
                        $lz=0;
                        $dc[$y][100-$i]=0;
                        $lc++;
                    }else{
                        $lz++;
                        $dc[$y][100-$i]=1;
                        $lc=0;
                        $zq[$y]++;
                    }
                }
                // 预测模式为杀尾数
                if ($data['type']==3){
                    // 杀尾数判断
                    $ycdata[$y][$i]='杀'.$bg->GetWs($b[$y][$i]).'尾';
                    if ($bg->GetWs($b[$y][100-$i+1]) === $bg->GetWs($kjdata[100-$i]['c4'])){
                        $lz=0;
                        $dc[$y][100-$i]=0;
                        $lc++;
                    }else{
                        $lz++;
                        $dc[$y][100-$i]=1;
                        $lc=0;
                        $zq[$y]++;
                    }
                }
                // 预测模式为中边
                if ($data['type']==6){
                    // 中边判断
                    $ycdata[$y][$i]=$bg->GetZb($b[$y][$i]);
                    if ($bg->GetZb($b[$y][100-$i+1]) === $bg->GetZb($kjdata[100-$i]['c4'])){
                        $lz++;
                        $dc[$y][100-$i]=1;
                        $lc=0;
                        $zq[$y]++;
                    }else{
                        $lz=0;
                        $dc[$y][100-$i]=0;
                        $lc++;
                    }
                }
                // 判断连中连错
                $maxlz[$y]<=$lz?$maxlz[$y]=$lz:$maxlz[$y]=$maxlz[$y];
                $maxlc[$y]<=$lc?$maxlc[$y]=$lc:$maxlc[$y]=$maxlc[$y];
            }
            $cw[$y]=100-$zq[$y];
            // 判断是否推荐 1=>推荐;0=>不推荐
          $lz>=6?$sftj[$y]=1:$sftj[$y]=0;
        }
        $ds     =  "";
        $dx     =  "";
            $ds=$bg->GetDs($kjdata[0]['c4']);
            $dx=$bg->GetDx($kjdata[0]['c4']);
        return view('index', [
                'adv' =>  $adv,
                'info' =>  $info,
                'alg'   =>  $alg,
                'sftj'  =>  $sftj,
                'dc'    =>  $dc[$data['i']-1],
                'maxlz' =>  $maxlz[$data['i']-1],
                'maxlc' =>  $maxlc[$data['i']-1],
                'zq'    =>  $zq[$data['i']-1],
                'cw'    =>  $cw[$data['i']-1],
                'yc'    =>  $ycdata[$data['i']-1],
                'kjdata'   => $kjdata,
                'kjtype'    => $data['kjtype'],
                'type'      =>  $data['type'],
                'i'     =>  $data['i'],
                'ds'    =>  $ds,
                'dx'    =>  $dx,
        ]);
    }
    // 上传图片
    public function upload(){

        $file = request()->file('image');
        // 移动到框架应用根目录/uploads/ 目录下
        try{
            // 上传图片到本地服务器
            $saveName = \think\facade\Filesystem::disk('public')->putFile('img',$file);
            $arr = ['code' => 200, 'msg' => '图片上传成功！', 'path' =>  $saveName];
            return json($arr);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->exceptionHandle($e,'图片上传失败!' . $e->getMessage(),'json','');
        }

    }

}
